package edu.hncj.seckill.service.impl;

import edu.hncj.seckill.dao.BusinessMapper;
import edu.hncj.seckill.dao.OrderMapper;
import edu.hncj.seckill.dao.ProductMapper;
import edu.hncj.seckill.dao.UserMapper;
import edu.hncj.seckill.domain.Business;
import edu.hncj.seckill.domain.Order;
import edu.hncj.seckill.domain.Product;
import edu.hncj.seckill.domain.User;
import edu.hncj.seckill.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author gcl
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    BusinessMapper businessMapper;

    @Override
    public Product selectProductByPid(String pid) {
        return productMapper.selectById(pid);
    }

    @Override
    public int updateProduct(Product product) {
        return productMapper.updateProduct(product);
    }

    @Override
    public Order createOrder(Product product, String userId) {
        Order order = new Order();
        //先设置Order自身的参数(订单状态默认是待支付)// 0 待支付  1 待发货 2 已发货  3 取消
        order.setOrderStatus(0);
        order.setId(UUID.randomUUID().toString().replace("-", ""));
        order.setCreateTime(new Date());
        // 设置商品属性
        order.setProductId(product.getId());
        order.setProductName(product.getName());
        order.setTotalPrice(product.getPrice());
        // 设置用户属性
        order.setUserId(userId);
        User user = userMapper.selectByPrimaryKey(userId);
        order.setUserName(user.getName());
        order.setUserTelphone(user.getTelphone());
        // 设置订单总的商家属性
        Business business = businessMapper.selectByPrimaryKey(product.getBusinessId());
        order.setBusinessId(product.getBusinessId().toString());
        order.setBusinessName(business.getUserName());
        // 设置参数完成，准备添加订单
        int a = orderMapper.insert(order);
        return a == 0 ? null:order  ;
    }

    @Override
    public List<Order> getOrderListByUserId(String userId) {
        return orderMapper.getOrderListByUserId(userId);
    }

}
